package servlets;

import java.io.IOException;
import java.security.MessageDigest;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Errors;
import exceptions.GebruikerBestaatAlException;
import exceptions.GebruikerBestaatNietException;

/**
 * Servlet implementation class InlogServlet
 */
public class InlogServlet extends BasisServlet
{

	private String encodeMD5(String md5)
	{
		try
		{
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < array.length; ++i)
			{
				buffer.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
			}
			return buffer.toString();
		} catch (java.security.NoSuchAlgorithmException e)
		{

		}
		return null;
	}

	private void registreer(HttpServletRequest request, HttpServletResponse response, HttpSession session)
	{
		if (!request.getParameter("username").toString().equals("")
				&& !request.getParameter("password").toString().equals("")
				&& !request.getParameter("password2").toString().equals("")
				&& !request.getParameter("mail").toString().equals("")
				&& !request.getParameter("adress").toString().equals("")
				&& !request.getParameter("birthday").toString().equals("")
				&& request.getParameter("password").toString().equals(request.getParameter("password2")))
		{
			try
			{
				String encPass = encodeMD5(request.getParameter("password"));
				gDao.addGebruiker(request.getParameter("username"), request.getParameter("adress"),
						request.getParameter("birthday"), request.getParameter("mail"), encPass);

				melding("Registreren voltooid!", request, response);
			} catch (GebruikerBestaatAlException e)
			{
				melding(Errors.E003.getErorr(), request, response);
			} catch (Exception e)
			{
				melding(Errors.E001.getErorr(), request, response);
			}
		} else
		{
			melding(Errors.E001.getErorr(), request, response);
		}
	}
	private void login(HttpServletRequest request, HttpServletResponse response, HttpSession session)
	{
		if (!request.getParameter("mail").toString().equals("")
				&& !request.getParameter("password").toString().equals(""))
		{
			try
			{
				String encPass = encodeMD5(request.getParameter("password"));
				gDao.logIn(request.getParameter("mail").toString(), encPass);
				session.setAttribute("mail", request.getParameter("mail").toString());
				login(request);

				melding("Inloggen is voltooid!", request, response);

			} catch (GebruikerBestaatNietException e)
			{
				melding(Errors.E002.getErorr(), request, response);
			} catch (Exception e)
			{
				melding(Errors.E001.getErorr(), request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected synchronized void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		login(request);
		String action = request.getParameter("action");

		if (action != null)
		{
			if (action.equals("logout"))
			{
				HttpSession session = request.getSession();
				if (session.getAttribute("mail") != null)
				{
					session.removeAttribute("mail");
					melding("Het uitloggen is voltooid!", request, response);
					return;
				} else
				{
					melding("Kan niet uitloggen als je nog niet ingelogd bent!", request, response);
					return;
				}

			} else if (action.equals("regist"))
			{
				RequestDispatcher view = request.getRequestDispatcher("registreren.jsp");
				view.forward(request, response);
			} else
			{
				melding("Geen geldige actie",request,response);
				return;
			}

		} else
		{
			RequestDispatcher view = request.getRequestDispatcher("inlog.jsp");
			view.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected synchronized void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException
	{
		login(request);
		String action = request.getParameter("action");
		HttpSession session = request.getSession();
		if (action != null)
		{
			if (action.equals("regist"))
			{
				registreer(request, response, session);
			} else if (action.equals("login"))
			{
				login(request, response, session);
			} else
			{
				melding(Errors.E001.getErorr(), request, response);
			}
		}
	}
}
